package com.suneast.suneaststore.modules.personal.collect;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.modules.goodsdetail.GoodsDetailFragment;
import com.suneast.suneaststore.modules.personal.collect.adapter.CollectGoodsAdapter;
import com.suneast.suneaststore.modules.personal.collect.adapter.CollectStoreAdapter;
import com.suneast.suneaststore.bean.personal.collect.CollectGoodsBean;
import com.suneast.suneaststore.bean.personal.collect.CollectStoreBean;
import com.suneast.suneaststore.modules.personal.collect.event.GotCollectedGoodsListResult;
import com.suneast.suneaststore.modules.personal.collect.event.GotCollectedStoreListResult;
import com.suneast.suneaststore.modules.common.Flag;
import com.suneast.suneaststore.modules.store.StoreFragment;
import com.suneast.suneaststore.ui.fragments.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CollectFragment extends BaseFragment {
    @Override
    public int setLayout() {
        return R.layout.fragment_collect;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        EventBus.getDefault().register(this);
        initData();
        initView();
        CollectHandler.getCollectedGoodsList(goodsPage + 1);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    private int goodsPage;
    private int storePage;

    private List<CollectGoodsBean> goodsBeanList;
    private List<CollectStoreBean> storeBeanList;

    private void initData() {
        goodsBeanList = new ArrayList<>();
        storeBeanList = new ArrayList<>();
    }

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.cb_edit_collect_list)
    CheckBox editCollectCb;
    @BindView(R.id.rg_collect_filter)
    RadioGroup collectFilterRg;
    @BindView(R.id.refresh_rv_collect)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.rv_collect)
    RecyclerView collectRv;
    @BindView(R.id.layout_edit)
    FrameLayout editLayout;
    @BindView(R.id.cb_select_all)
    CheckBox selectAllCb;
    private CollectGoodsAdapter goodsAdapter;
    private CollectStoreAdapter storeAdapter;

    private void initView() {
        toolbar.setNavigationOnClickListener(v -> pop());
        collectRv.setLayoutManager(new LinearLayoutManager(getContext()));
        goodsAdapter = new CollectGoodsAdapter(R.layout.item_collected_goods);
        goodsAdapter.setOnItemClickListener((BaseQuickAdapter adapter, View view, int position) ->
                {
                    CollectGoodsBean bean = (CollectGoodsBean) adapter.getData().get(position);
                    GoodsDetailFragment fragment = new GoodsDetailFragment().withId(bean.getId());
                    start(fragment);
                }
        );
        goodsAdapter.setOnItemChildClickListener((BaseQuickAdapter adapter, View view, int position)
                -> {
                    CollectGoodsBean bean = (CollectGoodsBean) adapter.getData().get(position);
                    GoodsDetailFragment fragment = new GoodsDetailFragment().withId(bean.getId());
                    start(fragment);
                }
        );
        storeAdapter = new CollectStoreAdapter(R.layout.item_collected_store);
        storeAdapter.setOnItemChildClickListener(
                (BaseQuickAdapter adapter, View view, int position) -> {
                    //打开店铺界面
                    CollectStoreBean bean = (CollectStoreBean) adapter.getData().get(position);
                    StoreFragment fragment = new StoreFragment().withId(bean.getId());
                    start(fragment);

                });
        storeAdapter.setOnItemClickListener((BaseQuickAdapter adapter, View view, int position) -> {
            //打开店铺界面
            CollectStoreBean bean = (CollectStoreBean) adapter.getData().get(position);
            StoreFragment fragment = new StoreFragment().withId(bean.getId());
            start(fragment);
        });
        collectRv.setAdapter(goodsAdapter);
        collectFilterRg.setOnCheckedChangeListener((RadioGroup radioGroup, int checkedId) -> {
            switch (checkedId) {
                case R.id.rb_collected_goods: {
                    collectRv.setAdapter(goodsAdapter);
                }
                break;
                case R.id.rb_collected_store: {
                    if (storeAdapter.getData().size() > 0) {
                        collectRv.setAdapter(storeAdapter);
                    } else {
                        CollectHandler.getCollectedStoreList(1);
                    }
                }
                break;
                case R.id.rb_collected_service: {

                }
                break;
            }
        });
        editCollectCb.setOnCheckedChangeListener((CompoundButton compoundButton, boolean isChecked) -> {
            //显示勾选按钮
            editCollectCb.setText(isChecked ?
                    getResources().getString(R.string.finish)
                    : getResources().getString(R.string.edit));
            editLayout.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            switch (collectFilterRg.getCheckedRadioButtonId()) {
                case R.id.rb_collected_goods: {
                    goodsAdapter.setShowCheckBox(isChecked);
                    goodsAdapter.setNewData(goodsBeanList);
                }
                break;
                case R.id.rb_collected_store: {

                }
                break;
                case R.id.rb_collected_service: {

                }
                break;
            }
        });
        selectAllCb.setOnCheckedChangeListener((CompoundButton compoundButton, boolean isChecked) -> {
            //todo 勾选或者取消勾选全部收藏列表内容
        });

        mRefreshLayout.setEnableRefresh(true);//启用下拉刷新功能
        mRefreshLayout.setEnableLoadMore(true);//启用上拉加载功能
        mRefreshLayout.setEnableScrollContentWhenLoaded(true);//在加载完成时滚动列表显示新的内容
        mRefreshLayout.setDragRate(0.5f);

        //下拉刷新监听
        mRefreshLayout.setOnRefreshListener((RefreshLayout refreshLayout) -> {
            refreshLayout.getLayout().postDelayed(() -> {
                switch (collectFilterRg.getCheckedRadioButtonId()) {
                    case R.id.rb_collected_goods: {
                        goodsPage = 0;
                        CollectHandler.getCollectedGoodsList(1);
                    }
                    break;
                    case R.id.rb_collected_store: {
                        storePage = 0;
                        CollectHandler.getCollectedStoreList(1);
                    }
                    break;
                    case R.id.rb_collected_service: {

                    }
                    break;
                }
                refreshLayout.finishRefresh();
                refreshLayout.setNoMoreData(false);

            }, 500);
        });

        //上拉加载更多监听
        mRefreshLayout.setOnLoadMoreListener((RefreshLayout refreshLayout) -> {
            switch (collectFilterRg.getCheckedRadioButtonId()) {
                case R.id.rb_collected_goods: {
                    CollectHandler.getCollectedGoodsList(goodsPage + 1);
                }
                break;
                case R.id.rb_collected_store: {
                    CollectHandler.getCollectedStoreList(storePage + 1);
                }
                break;
                case R.id.rb_collected_service: {

                }
                break;
            }
        });
    }

    @OnClick({R.id.btn_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_delete: {
                //todo 删除已勾选的收藏
            }
            break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotCollectedGoodsList(GotCollectedGoodsListResult event) {
        if (event.getResult() == Flag.Result.SUCCEED) {
            if (goodsPage == 0) {
                if (goodsBeanList.size() > 0) {
                    goodsBeanList.clear();
                }
            }
            if (event.getList().size() > 0) {
                goodsPage = event.getPage();
                goodsBeanList.addAll(event.getList());
                if (collectFilterRg.getCheckedRadioButtonId() == R.id.rb_collected_goods) {
                    goodsAdapter.setNewData(goodsBeanList);
                    if (!collectRv.getAdapter().equals(goodsAdapter)) {
                        collectRv.setAdapter(goodsAdapter);
                    }
                }
            } else {
                if (goodsPage == 0) {
                    Toast.makeText(_mActivity, "您还没有收藏商品", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Toast.makeText(_mActivity, "请求数据失败", Toast.LENGTH_SHORT).show();
        }
        mRefreshLayout.finishLoadMore();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGotCollectedStoreList(GotCollectedStoreListResult event) {
        if (event.getResult() == Flag.Result.SUCCEED) {
            if (storePage == 0) {
                if (storeBeanList.size() > 0) {
                    storeBeanList.clear();
                }
            }
            if (event.getList().size() > 0) {
                storePage = event.getPage();
                storeBeanList.addAll(event.getList());
                if (collectFilterRg.getCheckedRadioButtonId() == R.id.rb_collected_store) {
                    storeAdapter.setNewData(storeBeanList);
                    collectRv.setAdapter(storeAdapter);
                    if (!collectRv.getAdapter().equals(storeAdapter)) {
                        collectRv.setAdapter(storeAdapter);
                    }
                    Log.e(TAG, "onGotCollectedStoreList: setAdapter");
                }
                Log.e(TAG, "onGotCollectedStoreList: size() > 0");

            } else {
                if (storePage == 0) {
                    Toast.makeText(_mActivity, "您还没有收藏店铺", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Toast.makeText(_mActivity, "请求数据失败", Toast.LENGTH_SHORT).show();
        }
        mRefreshLayout.finishLoadMore();
    }
}
